'use strict';
/**
 * @name 莫凡服务端 业务层
 * @version 0.0.1
 * @author ljr
 * @Time 2020年10月21日11:06:27
 */
const Service = require('egg').Service;

const operation_log = '[morefun:操作日志]';

class userService extends Service {
  async insert(data) {
    const {
      app,
      ctx
    } = this;
    let { user_nick, mix_nick, open_id, access_token } = data
    let params = {
      ...data,
      gmt_create: new Date(),
      gmt_modified: new Date(),
      ismain: false,
      admin: false
    };

    let isHaveSql = `SELECT count(0) as count FROM user where user_nick='${user_nick}'`
    let isHave = await app.mysql.query(isHaveSql, '');
    // 判断用户是否存在
    if (!isHave[0].count) {
      if (user_nick.indexOf(":") != -1) {      
        let mainSql = `SELECT  id,session_id  FROM user where user_nick='${user_nick.split(":")[0]}'`
        let main = await app.mysql.query(mainSql, '');
        // 判断主账号是否存在
        console.log(main,"xxxxxxxxxxxxxxx",main.length==0 || !main[0].session_id)
        if (main.length==0 || !main[0].session_id) {
          return { isOk: 0, msg: '请先用主账号登录' }
        } else {
          params.session_id = main[0].session_id
          params.main_user = main[0].id
        }
      } else {
        params.ismain = true
        // 添加session
        let sessionP = {
          session: access_token,
          gmt_create: new Date(),
          gmt_modified: new Date(),
        }
        let createSession = await app.mysql.insert('session', sessionP);
        if (createSession.insertId != 0) {
          params.session_id = createSession.insertId
        } else {
          return { isOk: 0, msg: '注册失败，请重试' }
        }
      }
      //添加用户记录
      let result = await app.mysql.insert('user', params);
      console.log('==================',result)
      if (result.insertId != 0) {
        return { isOk: 1, data :{ user_id: result.insertId, ...params} };
      } else {
        return { isOk: 0, msg: '注册失败，请重试' }
      }

    } else {
      return { isOk: 0, msg: '该用户已注册' }
    }
  }

  async findOne(data) {
    const {
      app,
      ctx
    } = this;
    let { user_nick, mix_nick,  open_id, access_token } = data
    let userSql = `SELECT id as user_id,main_user,user_nick,ismain,session_id,admin,avatar FROM user where user_nick='${user_nick}'`
    let user = await app.mysql.query(userSql, '');
    if(user.length) {
        let session=access_token;
        if(user[0].ismain) {
          let sessionSql = `update session set session='${access_token}',gmt_modified='${new Date()}' where id= '${user[0].session_id}'`
          await app.mysql.query(sessionSql,'');
        }else {
          let sessionSql = `SELECT session FROM session where id= ?`
          session = await app.mysql.query(sessionSql, user[0].session_id);
          session = session[0].session
        }

        return { isOk: 1, data:{...user[0] ,session} };
    } else {
      return { isOk: 0, msg: '没有此账号'};
    }
  }

  async findAll(data) {
    const {
      app,
      ctx
    } = this;
    let { curPage=1, page_size=10  } = data
    let count = await app.mysql.query(`SELECT count(0) as count FROM user`,'');
    let userSql = `SELECT id as user_id,main_user,session_id,user_nick, mix_nick,avatar, open_id,ismain,admin,gmt_create,gmt_modified FROM user limit ${(curPage-1)*page_size},${page_size}`
    let user = await app.mysql.query(userSql, '');

    return { isOk: 1, data:{ userLIst: user,curPage,page_size,pageTotal:count[0].count} };
  }



  async testPost(user_id) {
    const {
      app,
    } = this;
    const result = {};
    result.user_id = user_id;

    app.logger.info(`${operation_log}删除了id为${user_id}的用户`);

    return result;
  }
}

module.exports = userService;